﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Windows.Forms;

namespace DotNet.WinForm.Module
{
    using DotNet.Model;
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// FrmModulePermissionAdmin.cs
    /// 模块权限管理
    ///
    /// 修改记录
    ///
    ///     2008.06.17 版本：1.0 DotNet  模块权限管理功能页面编写。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2007.05.22</date>
    /// </author>
    /// </summary>
    public partial class FrmModulePermissionAdmin : BaseForm
    {
        private DataTable DTPermission = new DataTable(BasePermissionItemTable.TableName);     // 权限列表 DataTable

        // 模块主键
        private string TargetModuleId
        {
            set
            {
                this.ucModuleSelect.SelectedId = value;
            }
            get
            {
                return this.ucModuleSelect.SelectedId;
            }
        }

        // 模块名称
        private string TargetModuleFullName
        {
            set
            {
                this.ucModuleSelect.SelectedFullName = value;
            }
            get
            {
                return this.ucModuleSelect.SelectedFullName;
            }
        }

        private bool permissionAccess = false;                     // 访问权限
        private bool permissionAdd = false;                        // 新增权限
        private bool permissionEdit = false;                       // 编辑权限
        private bool permissionDelete = false;                     // 删除权限

        public FrmModulePermissionAdmin()
        {
            InitializeComponent();
        }

        public FrmModulePermissionAdmin(string moduleId)
            : this()
        {
            this.ucModuleSelect.SelectedId = moduleId;
        }

        public FrmModulePermissionAdmin(string moduleId, string moduleFullName)
            : this()
        {
            this.ucModuleSelect.SelectedId = moduleId;
            this.ucModuleSelect.SelectedFullName = moduleFullName;
        }

        #region public override void SetControlState() 设置按钮状态

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        public override void SetControlState()
        {
            // 设置选择角色用户控件状态
            this.ucModuleSelect.AllowNull = false;
            this.ucModuleSelect.SimpleAdmin = true;

            this.btnRemove.Enabled = false;
            this.btnSelectAll.Enabled = false;
            this.btnInvertSelect.Enabled = false;
            this.btnAddPermission.Enabled = this.permissionAdd;

            if (this.DTPermission.DefaultView.Count > 0)
            {
                this.btnSelectAll.Enabled = this.permissionEdit;
                this.btnInvertSelect.Enabled = this.permissionEdit;
                this.btnRemove.Enabled = this.permissionDelete;
            }
        }

        #endregion public override void SetControlState() 设置按钮状态

        #region public override void GetPermission() 获得页面的权限

        /// <summary>
        /// 获得页面的权限
        /// </summary>
        public override void GetPermission()
        {
            this.permissionAccess = this.IsAuthorized("ModuleAdmin");   // 访问权限
            this.permissionAdd = this.IsAuthorized("ModuleAdmin");      // 新增权限
            this.permissionEdit = this.IsAuthorized("ModuleAdmin");     // 编辑权限
            this.permissionDelete = this.IsAuthorized("ModuleAdmin");   // 删除权限
        }

        #endregion public override void GetPermission() 获得页面的权限

        #region private void BindData() 绑定屏幕数据

        /// <summary>
        /// 绑定屏幕数据
        /// </summary>
        private void BindData()
        {
            if (this.TargetModuleId.Length > 0)
            {
                this.ucModuleSelect.SelectedId = this.TargetModuleId;
                this.ucModuleSelect.SelectedFullName = this.TargetModuleFullName;
            }

            // 往DataTable中添加选择列
            BaseInterfaceLogic.DataTableAddColumn(this.DTPermission, BaseBusinessLogic.SelectedColumn);
            this.grdPermission.DataSource = this.DTPermission.DefaultView;
        }

        #endregion private void BindData() 绑定屏幕数据

        #region public override void FormOnLoad() 加载窗体

        /// <summary>
        /// 加载窗体
        /// </summary>
        public override void FormOnLoad()
        {
            // 获得数据
            this.DTPermission = ServiceManager.Instance.ModuleService.GetPermissionDT(UserInfo, this.TargetModuleId);

            // 绑定屏幕数据
            this.BindData();
        }

        #endregion public override void FormOnLoad() 加载窗体

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRowView dataRowView in this.DTPermission.DefaultView)
            {
                dataRowView.Row[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRowView dataRowView in this.DTPermission.DefaultView)
            {
                if (dataRowView.Row[BaseBusinessLogic.SelectedColumn].ToString().Equals(true.ToString()))
                {
                    dataRowView.Row[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    dataRowView.Row[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

        private void btnAddPermission_Click(object sender, EventArgs e)
        {
            // 用反射获得窗体
            string assemblyName = "DotNet.WinForm.PermissionItem";
            string formName = "FrmPermissionSelect";
            Form frmPermissionSelect = CacheManager.Instance.GetForm(assemblyName, formName);
            ((IBaseSelectPermissionForm)frmPermissionSelect).AllowNull = false;
            ((IBaseSelectPermissionForm)frmPermissionSelect).MultiSelect = true;
            if (frmPermissionSelect.ShowDialog() == DialogResult.OK)
            {
                ServiceManager.Instance.ModuleService.BatchAddPermissions(UserInfo, this.TargetModuleId, ((IBaseSelectPermissionForm)frmPermissionSelect).SelectedIds);

                // 加载窗体
                this.FormOnLoad();
            }
        }

        #region private string[] GetSelecteIds() 获得已被选择的主键数组

        /// <summary>
        /// 获得已被选择的主键数组
        /// </summary>
        /// <returns>主键数组</returns>
        private string[] GetSelecteIds()
        {
            return BaseInterfaceLogic.GetSelecteIds(this.DTPermission, BasePermissionItemTable.FieldId, BaseBusinessLogic.SelectedColumn);
        }

        #endregion private string[] GetSelecteIds() 获得已被选择的主键数组

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns>影响行数</returns>
        public int Remove()
        {
            int returnValue = 0;
            if (BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTPermission, BaseBusinessLogic.SelectedColumn))
            {
                if (DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.MSG0075, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    // 设置鼠标繁忙状态
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        // 调用接口方式实现
                        returnValue = ServiceManager.Instance.ModuleService.BatchDletePermissions(UserInfo, this.TargetModuleId, this.GetSelecteIds());
                        // 获取最新数据
                        this.DTPermission = ServiceManager.Instance.ModuleService.GetPermissionDT(UserInfo, this.TargetModuleId);

                        // 绑定屏幕数据
                        this.BindData();

                        // 设置按钮状态
                        this.SetControlState();
                    }
                    catch (Exception ex)
                    {
                        this.ProcessException(ex);
                    }
                    finally
                    {
                        // 设置鼠标默认状态
                        this.Cursor = Cursors.Default;
                    }
                }
            }
            return returnValue;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            this.Remove();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // 关闭窗体
            this.Close();
        }
    }
}